//
//  CustomStatueBar.m
//  CustomStatueBar
//
//  Created by 贺 坤 on 12-5-21.
//  Copyright (c) 2012年 深圳市瑞盈塞富科技有限公司. All rights reserved.
//

#import "CustomStatueBar.h"
#import "BanBu_AppDelegate.h"

#import "AppDataManager.h"
#import "BanBu_ChatViewController.h"
#import "LeveyTabBarController.h"
#import "BanBu_ThemeManager.h"

@implementation CustomStatueBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.windowLevel = UIWindowLevelNormal;
        self.windowLevel = UIWindowLevelStatusBar + 1.0f;
        self.backgroundColor = [UIColor clearColor];
        
        CGRect rect = [UIApplication sharedApplication].statusBarFrame;
        rect.size.width = 100;
        rect.origin.x = 0;
        
        defaultLabel = [[BBCyclingLabel alloc]initWithFrame:rect andTransitionType:BBCyclingLabelTransitionEffectScrollUp];
        
        
        defaultLabel.backgroundColor = [UIColor blackColor];
        
        if (IS_IOS_7)
        {
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTheme1) name:kThemeDidChangeNotification object:nil];
            [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setTextLabelBackgroundColor:) name:kThemeDidChangeAndIOS7Notification object:nil];
            [self changeTheme1];
        }
        
        defaultLabel.textAlignment = NSTextAlignmentCenter;
        
        defaultLabel.transitionDuration = 0.75;
        defaultLabel.font = [UIFont systemFontOfSize:15];
        defaultLabel.textColor = [UIColor colorWithWhite:1 alpha:0.8];
        defaultLabel.clipsToBounds = YES;
        defaultLabel.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(fullStatueBar)];
        [defaultLabel addGestureRecognizer:tap];
        [tap release];
        
        [self addSubview:defaultLabel];
        
        self.isShow = NO;
        self.isExist = NO;
    }
    return self;
}

-(void)changeTheme1
{
    NSString *navImageName = nil;
    if(IS_IOS_7)
    {
        navImageName = @"statusBar_ios7.png";
        defaultLabel.backgroundColor = [UIColor colorWithPatternImage:[ThemeManager getThemeImage:navImageName]];
    }
}

-(void)setTextLabelBackgroundColor:(NSNotification *)notifi
{
    NSString *navImageName = [[notifi object]valueForKey:@"color"];
    if ([navImageName isEqualToString:@"black"])
    {
        defaultLabel.backgroundColor = [UIColor blackColor];
    }
    else//回复为查看头像之前的颜色
    {
        if(IS_IOS_7)
        {
            navImageName = @"statusBar_ios7.png";
            defaultLabel.backgroundColor = [UIColor colorWithPatternImage:[ThemeManager getThemeImage:navImageName]];
        }
        else
        {
            defaultLabel.backgroundColor = [UIColor blackColor];
        }
    }
    
}

//显示默认状态栏
- (void)fullStatueBar
{
    self.isShow = !self.isShow;
    if (self.isShow)
    {
        [UIView beginAnimations:@"" context:nil];
        [UIView setAnimationDuration:1.0f];
        self.frame =CGRectMake(320, 0,40, 20);
        [UIView commitAnimations];
        
        [self gotoChatController];
        
        [self hide];
    }
    else
    {
        [UIView beginAnimations:@"" context:nil];
        [UIView setAnimationDuration:1.0f];
        self.frame = CGRectMake(220, 0, 100, 20);
        [UIView commitAnimations];
    }
}

-(void)reShowStatus:(NSString *)message
{
    self.hidden = NO;
    self.alpha = 1.0f;
    self.isShow = YES;
    if (message.length)
    {
        [defaultLabel setText:message animated:NO];
    }
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:1.0f];
    self.frame = CGRectMake(220, 0, 100, 20);
    [UIView commitAnimations];
    
    if ([self.hiddenTimer isValid])
    {
        [self.hiddenTimer invalidate];
        self.hiddenTimer = nil;
    }
    self.hiddenTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(hiddenDefaultText) userInfo:nil repeats:NO];
}

//初始化并显示自定义状态栏
- (void)showStatusMessage:(NSString *)message
{
    self.hidden = NO;
    self.alpha = 1.0f;
    self.isExist = YES;
    self.isShow = YES;
//    if (message.length)
//    {
//        [defaultLabel setText:message animated:NO];
//    }
    
    CGSize totalSize = self.frame.size;
    totalSize.width = 100;
    self.frame = (CGRect){ self.frame.origin, 0, totalSize.height };
    
        [UIView animateWithDuration:0.5f
                     animations:^{
                         self.frame = (CGRect){ self.frame.origin, totalSize };
                     }
                     completion:^(BOOL finished){
                         [defaultLabel setText:message animated:YES];
                     }
     ];
    
    if ([self.hiddenTimer isValid])
    {
        [self.hiddenTimer invalidate];
        self.hiddenTimer = nil;
    }
    self.hiddenTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(hiddenDefaultText) userInfo:nil repeats:NO];
}

-(void)hiddenDefaultText
{
    self.isShow = NO;
    self.alpha = 1.0f;
    
    [self changeMessge:@""];
    
        [UIView animateWithDuration:1.0f
                     animations:^{
                         self.alpha = 0.0f;
                     }
                     completion:^(BOOL finished){
                         [defaultLabel setText:nil animated:YES];
                         self.hidden = YES;
                     }
     ];
}

//隐藏
- (void)hide
{
    self.isShow = NO;
    self.alpha = 1.0f;
    
        [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished){
        defaultLabel.text = @"";
        self.hidden = YES;
    }];
}

//跳转到聊天界面
-(void)gotoChatController
{
    NSArray * userinfoArray = [MyAppDataManager getPersonInfo:@[self.userid]];
    NSDictionary * userInfoDic = nil;
    if (userinfoArray.count>0)
    {
        userInfoDic = [userinfoArray objectAtIndex:0];
    }
    if (userInfoDic == nil)
    {
        return;
    }
    
    NSInteger unreadNumber = [MyAppDataManager clearUnReadDialogsNumber:MyAppDataManager.chatuid];
    if (unreadNumber>5)
    {
        [MyAppDataManager readTalkList:self.userid WithNumber:unreadNumber];
    }
    else
    {
        [MyAppDataManager readTalkList:self.userid WithNumber:5];
    }

    BanBu_ChatViewController * chat = [[BanBu_ChatViewController alloc]initWithPeopleProfile:userInfoDic];
    
    BanBu_AppDelegate * app = (BanBu_AppDelegate *)[UIApplication sharedApplication].delegate;
    UINavigationController * nav = [app.tabbarController.viewControllers objectAtIndex:app.tabbarController.selectedIndex];
    
    [nav pushViewController:chat animated:YES];
    [chat release];
    NSDictionary *unReadDic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",0-unreadNumber],@"pushcount",nil];
    [AppComManager getBanBuData:Banbu_Set_User_Pushcount par:unReadDic delegate:self];
    
    //要刷新点对点页面
    [MyAppDataManager setUnreadNumber:0 With:self.userid];
    
    self.userid = @"";
    self.isShow = NO;
}

//更新显示内容
- (void)changeMessge:(NSString *)message
{
    [defaultLabel setText:message animated:YES];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangeAndIOS7Notification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangeNotification object:nil];
    [_hiddenTimer release],_hiddenTimer = nil;
    [_userid release],_userid = nil;
    [defaultLabel release],defaultLabel = nil;
    [super dealloc];
}
@end
